@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('/vendor/tyupload/control/css/zyUpload.css')}}" type="text/css" rel="stylesheet" />
    <link href="{{url('css/jquery.form.css')}}" type="text/css" rel="stylesheet" />
    <!-- 配置文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.config.js')}}"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.all.js')}}"></script>
@endsection

@section('html-body')
        <!-- content start -->
<div id="content">
    <div class="gg-step">
        <form  id="advform">
            <input type="hidden" name="id" value=""/>
            <div class="gg-list">
                <label class="tit">标题</label>
                <div class="cont">
                    <input class="gg-ipt" type="text" placeholder="请输入标题" name="title" id="title" value="@if($content){{$content->title}} @endif" /><cite class="required">*</cite>
                </div>
            </div>

            <div class="gg-list" data-id="ggzst2">
                <label class="tit">展示图</label>
                <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">
                    <!--  <input type="file"><br/> -->
                    <input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >
                    <div id="showimg">
                    </div>
                    <div class="btn" style="background: #00A2D4">
                        <span id="inputImage">添加图片</span>
                        <input id="fileupload" type="hidden" name="thumb_id" value="@if($content){{$content->img_id}} @endif">
                    </div>
                    <div class="progress">
                        <span class="bar"></span><span class="percent">0%</span >
                    </div>
                    <p class="text" id="uploadthumb">@if($content)图片id为：{{$content->img_id}} @else展示缩略图（图片格式为jpg或png）@endif</p>
                    <!--<div class="files"></div>-->
                </div>
            </div>

            <div class="gg-list">
                <label class="tit">文章内容</label>
                <div class="cont">
                    <textarea class="textarea" placeholder="请输入活动备注" name="adtips" id="adtips" style="border: none"></textarea>
                </div>
            </div>

        </form>
        <div class="gg-list" >
            <label class="tit">&nbsp;</label>
            <div class="cont" >
                <a href="javascript:;" class="next-step" id="nextid">提交</a>
                <input type="hidden" id="contentid" value="@if($content){{$content->id}} @else 0 @endif">
            </div>
        </div>
    </div>
</div>
<!-- content end -->

<script>

    $(function() {
        <!-- 实例化编辑器 -->
        var ue = UE.getEditor('adtips',{
            autoHeightEnabled: true,
            initialFrameHeight:400,
            toolbars: [
                ['fullscreen', 'source', 'undo','redo','|','fontsize','horizontal','|','simpleupload','removeformat'],
                ['bold', 'italic', 'underline', 'forecolor', 'backcolor','|','indent','|','justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight','|','insertunorderedlist', 'insertorderedlist','|', 'imagenone','imageleft','imageright','imagecenter','|','link']
            ]
        });
        jQuery.ajaxSetup({
            case:false,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        var doc = document,
                $doc = $(doc);

        @if($content)
        ue.ready(function() {
            ue.setContent('<?php echo $content->content ?>');
        });
        @endif
        //添加广告
        $doc.on('click','#nextid',function(){
            var title = $('#title').val();
            var imgid = $('#fileupload').val();
            var id    = $('#contentid').val().trim();
            var html = '';
            ue.ready(function() {
                html = ue.getContent();
            });
            $.ajax({
                type:"post",
                url:'{{route('Api.Article.buildArticle')}}',
                dataType:"json",
                data:{'id':id,'title':title,'img_id':imgid,'content':html},
                success:function(obj){
                    if(obj.level == "success"){
                        noty({text:obj.message,type:obj.level,timeout:600,
                            callback:{
                                afterClose:function(){
                                    location.reload();
                                }
                            }
                        });
                    }
                },
                error:function(obj){
                    var datajson = obj.responseJSON;
                    tusi(datajson.message);
                }
            });
        });

        //广告缩略图上传代码
        var uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4',
            browse_button: 'inputImage',
            url: '{{route('Api.Upload.Image')}}',

            chunk_size: '20mb',
            filters: {
                max_file_size: '5mb',
                mime_types: [
                    {title: "Image files", extensions: "jpg,gif,png,apk"}
                ],
                prevent_duplicates: false
            },
            multi_selection: true,
            unique_names: true,

            flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
            silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

            file_data_name: 'photo',
            headers: {
                Accept: 'application/json'
            },

            multipart_params: {
                _token: '{{ csrf_token() }}'
            },

            init: {
                PostInit: function () {
                },

                FilesAdded: function (up, files) {
                    up.start();
                },

                BeforeUpload: function (up, file) {
                    up.settings.multipart_params.file_name = file.name;
                },

                UploadProgress: function (up, file) {

                },

                Error: function (up, err) {
                    var responseObj = '', responseMessage = '';
                    try {
                        responseObj = $.parseJSON(err.response);
                        responseMessage = responseObj.message;
                    } catch ($e) {

                    }

                    noty({
                        type: 'error',
                        text: err.message + ' ' + responseMessage,
                        timeout: 800
                    });
                },

                FileUploaded: function (up, file, info) {
                    var responseObj = '', responseMessage = '';
                    try {
                        responseObj = $.parseJSON(info.response);
                        responseMessage = responseObj.message;
                    } catch ($e) {

                    }
                    $('#fileupload').val(responseObj.data.id);
                    $('#uploadthumb').html('<span style="color: green">上传成功</span>,'+responseObj.data.img_name);
                }
            }
        });
        uploader.init();
    });
</script>
@endsection